import torch 
import json

import matplotlib.pyplot as plt 
import numpy as np 
import nibabel as nib
from nibabel import nifti1
from nibabel.viewers import OrthoSlicer3D
import h5py
import scipy.io as scio

if __name__ == "__main__":

    #~~~~~~~~~~~~~~~~~~~~~~~~~ 将测试数据转为h5格式
    # save_dir = "./BraTS_test_h5/"
    # index = 0
    # test_file_name = ["BraTS446.mat", "BraTS447.mat", "BraTS455.mat", "BraTS461.mat", "BraTS463.mat"]
    # for each_file in test_file_name:
    #     img_mat = scio.loadmat("BraTS_test/imagesTest/" + each_file)
    #     img_data = img_mat["cropVol"]
    #     label_mat = scio.loadmat("BraTS_test/labelsTest/" + each_file)
    #     img_data = img_data.transpose(3, 2, 0, 1)
    #     label_data = label_mat["cropLabel"]
    #     label_data = label_data.transpose(2, 0, 1)
    #     print(img_data.shape)
    #     print(label_data.shape)
    #     f = h5py.File(save_dir + 'BraTS_val_' + str(index) + ".h5", 'w')
    #     f.create_dataset('raw', data=img_data, compression='gzip')
    #     f.create_dataset('label', data=label_data, compression='gzip')
    #     f.close()
    #     index += 1
    #~~~~~~~~~~~~~~~~~~~~~~~~~~``

    #~~~~~~~~~~~~~~~~~~~ 将训练数据转为h5类型的格式
    # save_dir = "./BraTS_train_h5/"
    # with open("./BraTS_train/dataset.json", "r") as f:
        
    #     data_json = json.loads(f.read())
    #     print(data_json["training"])
    # index = 0
    # for each_dict in data_json["training"]:
    #     img_path = each_dict["image"]
    #     label_path = each_dict["label"]

    #     img = nib.load("./BraTS_train/" + img_path[2:])
    #     label = nib.load("./BraTS_train/" + label_path[2:])
    #     # print(img)
    #     # print(img.header['db_name'])  # 输出头信息
    #     data = np.array(img.dataobj)
    #     label_data = np.array(label.dataobj)
    #     # print(data.shape)
    #     data = data.transpose(3, 2, 0, 1)
    #     label_data = label_data.transpose(2, 0, 1)
    #     print(data.shape)
    #     print(label_data.shape)
    #     f = h5py.File(save_dir + 'BraTS_' + str(index) + ".h5", 'w')
    #     f.create_dataset('raw', data=data, compression='gzip')
    #     f.create_dataset('label', data=label_data, compression='gzip')
    #     f.close()
    #     index += 1

    #~~~~~~~~~~~~~~~~~``



    # #shape有四个参数 patient001_4d.nii.gz
    # #shape有三个参数 patient001_frame01.nii.gz   patient001_frame12.nii.gz
    # #shape有三个参数  patient001_frame01_gt.nii.gz   patient001_frame12_gt.nii.gz
    # # channel, width, height, queue = img.dataobj.shape
    # print(img.dataobj.shape)
    # # OrthoSlicer3D(img.dataobj[:, :, :, 0]).show()
    # plt.imshow(img.dataobj[:, :, 50, 0], cmap="gray")
    # plt.show()

